package ribbitmq_2;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

//工作模式发生消息
public class Producer_2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory c = new ConnectionFactory();
        c.setHost("192.168.64.140");
        c.setPort(5672);
        c.setUsername("admin");
        c.setPassword("admin");

        Connection ch = c.newConnection();
        Channel cha = ch.createChannel();
        //定义持久化的队列(第二个改为false为关闭删除)
        cha.queueDeclare("task_queue",
                true,      //是否持久队列(默认关闭服务器时销毁)
                false,      //是否排他队列
                false,      //是否自动删除
                null);

        //定义循环发送消息
        while (true){
            System.out.println("请输入消息");
            String msg = new Scanner(System.in).nextLine();

            if (msg.equals("byby")){    //结束条件
                break;
            }

            cha.basicPublish("",
                    "task_queue",    //队列名
                    MessageProperties.PERSISTENT_TEXT_PLAIN,   //其他属性消息持久化
                    msg.getBytes());//转换字节

            System.out.println("发生成功");
        }

        cha.close();
        ch.close();
    }
}
